<?php
session_start();

// 检查用户是否登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}

$currentUser = $_SESSION['username'];

// 钱包数据文件
$walletFile = __DIR__.'/money/info.json';

// 初始化钱包数据
if (!file_exists($walletFile)) {
    $walletData = ['users' => []];
    file_put_contents($walletFile, json_encode($walletData));
} else {
    $walletData = json_decode(file_get_contents($walletFile), true);
}

// 检查用户是否已开通钱包
$userHasWallet = isset($walletData['users'][$currentUser]) && $walletData['users'][$currentUser]['activated'];

// 处理开通钱包请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['activate_wallet'])) {
    if (!$userHasWallet) {
        $walletData['users'][$currentUser] = [
            'balance' => 0.00,
            'activated' => true,
            'last_signin' => null
        ];
        file_put_contents($walletFile, json_encode($walletData, JSON_PRETTY_PRINT));
        $userHasWallet = true;
        $successMessage = "钱包开通成功！";
    }
}

// 获取用户余额
$balance = $userHasWallet ? $walletData['users'][$currentUser]['balance'] : 0.00;
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的钱包 - JIIL微聊</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            color: #333;
            min-height: 100vh;
        }
        
        /* 白色顶部栏 */
        .top-bar {
            background: white;
            padding: 16px 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            display: flex;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #000;
            font-size: 20px;
            margin-right: 15px;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #f0f2f5;
        }
        
        .page-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            flex-grow: 1;
            text-align: center;
        }
        
        .container {
            max-width: 600px;
            margin: 80px auto 30px;
            padding: 0 20px;
        }
        
        /* 钱包卡片 */
        .wallet-card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            margin-bottom: 25px;
            text-align: center;
        }
        
        .wallet-header {
            margin-bottom: 30px;
        }
        
        .wallet-header h1 {
            font-size: 28px;
            color: #333;
            margin-bottom: 10px;
        }
        
        .wallet-header p {
            color: #666;
            font-size: 16px;
        }
        
        /* 主功能按钮 */
        .main-buttons {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }

        .big-button {
            width: 100%;
            padding: 22px;
            background: #000;
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }

        .big-button:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            background: #222;
        }
        
        .big-button i {
            font-size: 24px;
        }

        /* 开通钱包提示 */
        .activate-prompt {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
            text-align: center;
            margin-bottom: 25px;
        }
        
        .activate-prompt h2 {
            font-size: 22px;
            font-weight: 600;
            color: #333;
            margin-bottom: 15px;
        }
        
        .activate-prompt p {
            color: #666;
            margin-bottom: 25px;
            line-height: 1.6;
        }
        
        .activate-btn {
            background: #000;
            color: white;
            border: none;
            border-radius: 30px;
            padding: 14px 45px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        
        .activate-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
        }
        
        /* 服务区域 */
        .services-section {
            background: white;
            border-radius: 20px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
        }
        
        .section-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #f0f2f5;
        }
        
        .services-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 15px;
        }
        
        .service-btn {
            background: #f8f9fa;
            border: none;
            border-radius: 15px;
            padding: 20px 15px;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .service-btn:hover {
            background: #f0f2f5;
            transform: translateY(-3px);
        }
        
        .service-btn i {
            font-size: 28px;
            color: #3b82f6;
        }
        
        .service-btn span {
            font-size: 14px;
            font-weight: 500;
            color: #333;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .main-buttons {
                grid-template-columns: 1fr;
            }
            
            .services-grid {
                grid-template-columns: repeat(3, 1fr);
            }
        }
    </style>
</head>
<body>
    <!-- 白色顶部栏 -->
    <div class="top-bar">
        <button class="back-btn" onclick="history.back()">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="page-title">JIIL钱包</div>
    </div>
    
    <div class="container">
        <?php if (isset($successMessage)): ?>
            <div class="notification">
                <i class="fas fa-check-circle"></i> <?= $successMessage ?>
            </div>
        <?php endif; ?>
        
        <?php if (!$userHasWallet): ?>
            <!-- 开通钱包提示 -->
            <div class="activate-prompt">
                <h2>开通钱包功能</h2>
                <p>开通钱包功能后，您可以进行收付款、查看余额、每日签到获得奖励等操作。</p>
                <form method="POST">
                    <button type="submit" name="activate_wallet" class="activate-btn">
                        <i class="fas fa-wallet"></i> 立即开通
                    </button>
                </form>
            </div>
        <?php else: ?>
            <!-- 余额显示 -->
            <div class="balance-display">
                <div class="balance-label">可用余额</div>
                <div class="balance-amount"><?= number_format($balance, 2) ?></div>
            </div>
            
            <!-- 主功能按钮 -->
            <div class="main-buttons">
                <a href="money/payment.php?action=payment" class="big-button">
                    <i class="fas fa-qrcode"></i> 付款
                </a>
                <a href="money/payment.php?action=receive" class="big-button">
                    <i class="fas fa-money-bill-wave"></i> 收款
                </a>
                <a href="money/signin.php" class="big-button">
                    <i class="fas fa-calendar-check"></i> 每日签到
                </a>
                <a href="money/balance.php" class="big-button">
                    <i class="fas fa-receipt"></i> 钱包明细
                </a>
            </div>
            
            <!-- 服务区域 -->
            <div class="services-section">
                <h2 class="section-title">服务</h2>
                <div class="services-grid">
                    <a href="money/payment.php?action=receive" class="service-btn">
                        <i class="fas fa-qrcode"></i>
                        <span>收款</span>
                    </a>
                    <a href="money/payment.php?action=payment" class="service-btn">
                        <i class="fas fa-money-bill-wave"></i>
                        <span>付款</span>
                    </a>
                    <a href="money/signin.php" class="service-btn">
                        <i class="fas fa-calendar-check"></i>
                        <span>签到</span>
                    </a>
                    <a href="money/balance.php" class="service-btn">
                        <i class="fas fa-history"></i>
                        <span>明细</span>
                    </a>
                </div>
            </div>
        <?php endif; ?>
        
        <div class="system-tip">
            本服务由JIIL微聊提供
        </div>
    </div>
</body>
</html>